package com.muy.redis.view.window.redis.view.table;

import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.NlsContexts;
import com.intellij.util.ui.ColumnInfo;
import com.muy.redis.utils.JacksonUtils;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;

/**
 * @Author jiyanghuang
 * @Date 2022/8/3 23:32
 */
public class TableColumnInfo extends ColumnInfo {

    private String key;

    private static final TableCellRenderer MR_TABLE_CELL_RENDERER = new MRTableCellRenderer();
    private static final MongoValueCellEditor defaultEditor = new MongoValueCellEditor();
    private static final DefaultCellEditor defaultEditorV = new DefaultCellEditor(new JTextField());



    private final CellEditorText defaultJsonEditor;
    private final CellEditorTextV2 defaultJsonEditorV2;

    private Project project;

    private boolean cellEditable;

    /**
     *
     * @param columnNames 每一列描述
     * @param items 数据List
     */
    public TableColumnInfo(@NlsContexts.ColumnName String name, Project project, boolean cellEditable) {
        super(name);
        this.key = name;
        this.project = project;
        this.cellEditable = cellEditable;
        defaultJsonEditor = new CellEditorText(project);
        defaultJsonEditorV2 = new CellEditorTextV2(project);
    }

    /**
     * 获取到的是每一个方格的描述
     * @param o List 中的一个元素，对于封装而言就是 TreeLineWrap
     * @return 返回List 中的一个元素，取出该元素中该列的值，这里就是每个方格的描述对象 ColumnDesc
     */
    @Nullable
    @Override
    public Object valueOf(Object o) {
        if(o instanceof TreeLineWrap){
            TreeLineWrap treeLineWrap = (TreeLineWrap)o;
            return treeLineWrap.getCm().get(key);
        }
        return "empty";
    }

    /**
     * 每一个方格怎么渲染，所以传的对象是方格描述对象
     * @param o 方格中的对象，这里可以路由，根据传的对象决定渲染对象
     * @return
     */
    @Override
    public @Nullable TableCellRenderer getRenderer(Object o) {
        return MR_TABLE_CELL_RENDERER;
    }

    /**
     * 方格对象决定是否可编辑
     * @param o
     * @return
     */
    @Override
    public boolean isCellEditable(Object o) {
        return cellEditable;
    }

    /**
     * 当可编辑时，点击方格时，编辑器
     * @param o
     * @return
     */
    @Override
    public @Nullable TableCellEditor getEditor(Object o) {
        if(o instanceof TreeLineWrap){
            TreeLineWrap treeLineWrap = (TreeLineWrap)o;
            ColumnDesc columnDesc = (ColumnDesc)treeLineWrap.getCm().get(key);
//            if(ColumnValueEnum.JSON.equals(columnDesc.getColumnValueEnum())){
//                return defaultJsonEditorV2;
//            }else if(ColumnValueEnum.ESCAPE_JSON.equals(columnDesc.getColumnValueEnum())){
//                return defaultJsonEditorV2;
//            }
            if(JacksonUtils.jsonStr(columnDesc.getValueShow())){
                return defaultJsonEditorV2;
            }
            return defaultEditor;
        }
        return defaultJsonEditorV2;
    }

    /**
     *
     * @param o TreeLineWrap
     * @param value getCellEditorValue 从该方法返回的值
     */
    @Override
    public void setValue(Object o, Object value) {
//        System.out.println("o --> " + o);
        if(o instanceof TreeLineWrap){
            TreeLineWrap treeLineWrap = (TreeLineWrap)o;
            ColumnDesc columnDesc = (ColumnDesc)treeLineWrap.getCm().get(key);
            String newValue = columnDesc.getColumnValueEnum().transferValue(value);
            if(newValue.equals(columnDesc.getValueShow())){
                return;
            }
            columnDesc.setValueShow(newValue);
            TreeLineObject old = treeLineWrap.getT().copyThis();
            treeLineWrap.updateValueToObj(columnDesc, (String)value, treeLineWrap.getT());
            TreeLineObject update = treeLineWrap.getT().copyThis();
            treeLineWrap.invokeVcFun(old, update, columnDesc);
        }
//        System.out.println("value --> " + o);
    }
}
